package com.tools.mytools.serialization;

import android.os.Bundle;
import android.util.Log;

import com.tools.common.aop.inputcheck.InputCheckTest;
import com.tools.common.aop.permission.RequirePermission;
import com.tools.common.serialize.FileRepo;
import com.tools.common.serialize.IRepo;
import com.tools.common.serialize.SharedPrefRepo;
import com.tools.mytools.BaseActivity;
import com.tools.mytools.R;

import butterknife.OnClick;

public class SerActivity extends BaseActivity {
    private User mUser = null;
    private IRepo mRepo = null;

    @Override
    protected int getLayoutResId() {
        return R.layout.ser_test_layout;
    }

    @Override
    protected void doActivityInit(Bundle savedInstanceState) {
        super.doActivityInit(savedInstanceState);

        mUser = new User();
        mUser.name = "KKK";
        mUser.country = "China";
        mUser.age = 23;

//        mRepo = SharedPrefRepo.instance();
        mRepo = FileRepo.instance();
    }

    @OnClick(R.id.do_ser) public void save() {
        doSaveUser();
    }

    @OnClick(R.id.do_des) public void load() {
        doLoadUser();
    }

    @OnClick(R.id.do_check) public void doCheck() {
        InputCheckTest.doTest("joe@gmail.com", "123456");
    }

    @RequirePermission(value="android.permission.WRITE_EXTERNAL_STORAGE")
    void doSaveUser() {
        Log.d("SerActivity", "save - " + mUser.toString());
        mRepo.store("user_info", mUser);
    }

    @RequirePermission(value="android.permission.WRITE_EXTERNAL_STORAGE")
    void doLoadUser() {
        User user = mRepo.load("user_info", User.class);
        if(user != null) {
            Log.d("SerActivity", "load - " + mUser.toString());
        } else {
            Log.d("SerActivity", "load - null");
        }
    }
}
